package com.shizhanzhe.gxb.my.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.my.bean.IncomeFormBean;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author YJ
 * @date 2019/1/8
 */
public class IncomeFormFragment extends Fragment {

    @BindView(R.id.mysy)
    TextView mysy;
    @BindView(R.id.my_ygsy)
    TextView myYgsy;
    @BindView(R.id.my_num)
    TextView myNum;
    @BindView(R.id.team_ygsy)
    TextView teamYgsy;
    @BindView(R.id.team_num)
    TextView teamNum;
    @BindView(R.id.vip_ygsy)
    TextView vipYgsy;
    @BindView(R.id.vip_num)
    TextView vipNum;
    Unbinder unbinder;
    private String id;
    private Context mContext;

    public static IncomeFormFragment newInstance(String id) {

        Bundle args = new Bundle();
        args.putString("id", id);
        IncomeFormFragment fragment = new IncomeFormFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getString("id");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_income_form, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getIncome();
    }

    private void getIncome() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.incomeStatement(UserBean.uid, Integer.parseInt(id)), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        IncomeFormBean incomeFormBean = new Gson().fromJson(json, IncomeFormBean.class);
                        mysy.setText(incomeFormBean.getIncome()+"");
                        myYgsy.setText("预估收入\n" + incomeFormBean.getMyorder().getIncome() + "元");
                        myNum.setText("订单数\n"+incomeFormBean.getMyorder().getNum());
                        teamYgsy.setText("预估收入\n" + incomeFormBean.getTeamorder().getIncome() + "元");
                        teamNum.setText("订单数\n"+incomeFormBean.getTeamorder().getNum());
                        vipYgsy.setText("团队贡献预估收入\n" + incomeFormBean.getTeamgx().getIncome() + "元");
                        vipNum.setText("团队贡献订单数\n"+incomeFormBean.getTeamgx().getNum());
                    } catch (Exception e) {
                        Log.d("____",json);
                        Log.d("_____",e.toString());
                        Toast.makeText(mContext, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext= activity;
    }
}
